<% content_for :title, @agent.name -%>

<div class='container agent-show'>
  <div class='row'>
    <div class='col-md-2'>
        <ul class="nav nav-pills nav-stacked" id="show-tabs">
          <li><%= link_to icon_tag('glyphicon-chevron-left') + ' Back'.html_safe, filtered_agent_return_link || agents_path %></li>

          <% if agent_show_view(@agent).present? %>
            <li class='active'><a href="#summary" data-toggle="tab"><span class='glyphicon glyphicon-picture'></span> Summary</a></li>
            <li><a href="#details" data-toggle="tab"><span class='glyphicon glyphicon-indent-left'></span> Details</a></li>
          <% else %>
            <li class='disabled'><a><span class='glyphicon glyphicon-picture'></span> Summary</a></li>
            <li class='active'><a href="#details" data-toggle="tab"><span class='glyphicon glyphicon-indent-left'></span> Details</a></li>
          <% end %>

          <li><a href="#logs" data-toggle="tab" data-agent-id="<%= @agent.id %>" class='<%= @agent.recent_error_logs? ? 'recent-errors' : '' %>'><span class='glyphicon glyphicon-list-alt'></span> Logs</a></li>

          <% if @agent.can_create_events? && @agent.events_count > 0 %>
            <li><%= link_to icon_tag('glyphicon-random') + ' Events'.html_safe, agent_events_path(@agent, return: request.fullpath) %></li>
          <% else %>
            <li class='disabled'><a><span class='glyphicon glyphicon-random'></span> Events</a></li>
          <% end %>

          <li class="dropdown">
            <a class="dropdown-toggle" data-toggle="dropdown" href="#"><span class="glyphicon glyphicon-th-list"></span> Actions <span class="caret"></span></a>
            <%= render 'agents/action_menu', agent: @agent, return_to: "show" %>
          </li>
        </ul>
      </div>

      <div class='col-md-10'>
        <div class="tab-content">
          <div class="tab-pane <%= agent_show_view(@agent).present? ? "active" : "disabled" %>" id="summary">
            <h2><%= @agent.name %> Summary</h2>

            <% if agent_show_view(@agent).present? %>
              <div class="show-view <%= agent_show_class(@agent) %>">
                <%= render agent_show_view(@agent) %>
              </div>
            <% end %>
          </div>

          <div class="tab-pane" id="logs" data-agent-id="<%= @agent.id %>">
            <div class='row'>
              <div class='col-md-12'>
                <h2>
                  <%= @agent.name %> Logs
                  <small>
                    <%= image_tag "spinner-arrows.gif", class: "spinner" %>
                    <span class="glyphicon glyphicon-refresh action-icon refresh"></span>
                    <span class="glyphicon glyphicon-trash action-icon clear"></span>
                  </small>
                </h2>
              </div>
            </div>
            <div class='row'>
              <div class='col-md-12'>
                <div class='logs'>
                  Just a moment...
                </div>
              </div>
            </div>
          </div>

          <div class="tab-pane <%= agent_show_view(@agent).present? ? "" : "active" %>" id="details">
            <h2><%= @agent.name %> Details</h2>

            <p>
              <b>Type:</b>
              <%= @agent.short_type.titleize %>
            </p>

            <% if @agent.can_be_scheduled? %>
              <p>
                <b>Schedule:</b>
                <%= agent_schedule(@agent) %>
              </p>

              <p>
                <b>Last checked:</b>
                <%= @agent.last_check_at ? time_ago_in_words(@agent.last_check_at) + " ago" : "never" %>
              </p>
            <% end %>

            <% if (agents = @agent.controllers).length > 0 %>
              <p>
                <b>Controllers:</b>
                <%= agents.map { |agent| link_to(agent.name, agent_path(agent)) }.to_sentence.html_safe %>
              </p>
            <% end %>

            <% if @agent.can_create_events? %>
              <p>
                <b>Keep events:</b>
                <%= (Agent::EVENT_RETENTION_SCHEDULES.detect {|s| s.last == @agent.keep_events_for } || [@agent.keep_events_for]).first %>
              </p>

              <p>
                <b>Last event created:</b>
                <%= @agent.last_event_at ? time_ago_in_words(@agent.last_event_at) + " ago" : "never" %>
              </p>
            <% end %>

            <% if @agent.can_receive_events? %>
              <p>
                <b>Last received event:</b>
                <%= @agent.last_receive_at ? time_ago_in_words(@agent.last_receive_at) + " ago" : "never" %>
              </p>
            <% end %>

            <% if @agent.can_create_events? %>
              <p>
                <b>Events created:</b>
                <%= link_to @agent.events_count, agent_events_path(@agent) %>
              </p>
            <% end %>

            <% if @agent.try(:oauthable?) %>
              <p>
                <b>Service:</b>
                <%= service_label(@agent.service) %>
              </p>
            <% end %>

            <% if @agent.can_receive_events? %>
              <p>
                <b>Event sources:</b>
                <% if (agents = @agent.sources).length > 0 %>
                  <%= agents.map { |agent| link_to(agent.name, agent_path(agent)) }.to_sentence.html_safe %>
                <% else %>
                  None
                <% end %>
              </p>

              <p>
                <b>Propagate immediately:</b>
                <%= yes_no @agent.propagate_immediately %>
              </p>
            <% end %>

            <% if @agent.can_create_events? %>
              <p>
                <b>Event receivers:</b>
                <% if (agents = @agent.receivers).length > 0 %>
                  <%= agents.map { |agent| link_to(agent.name, agent_path(agent)) }.to_sentence.html_safe %>
                <% else %>
                  None
                <% end %>
              </p>
            <% end %>

            <% if @agent.can_control_other_agents? %>
              <p>
                <b>Control Targets:</b>
                <% if (agents = @agent.control_targets).length > 0 %>
                  <%= agents.map { |agent| link_to(agent.name, agent_path(agent)) }.to_sentence.html_safe %>
                <% else %>
                  None
                <% end %>
              </p>
            <% end %>

            <p>
              <b>Working:</b>
              <%= working @agent %>
            </p>

            <p>
              <b>Options:</b>
              <pre><%= Utils.pretty_jsonify @agent.options || {} %></pre>
            </p>
            <% if @agent.memory.present? %>
              <p id="memory" data-agent-id="<%= @agent.id %>">
                <b>Memory:</b>
                <i class="fa-solid fa-spinner fa-spin-pulse spinner"></i>
                <i class="fa-solid fa-trash action-icon clear"></i>
                <a id="toggle-memory" href="#">Show</a>
                <pre class="memory hidden"><%= Utils.pretty_jsonify @agent.memory || {} %></pre>
              </p>
            <% end %>
          </div>
        </div>
      </div>
  </div>
</div>

